/**
 * 
 */
package ads.engenharia.bl;

import java.sql.SQLException;
import java.util.ArrayList;

import ads.engenharia.dal.ProfessorCompetenceDAO;
import ads.engenharia.dal.ProfessorDAO;
import ads.engenharia.dal.ProfessorScheduleDAO;
import ads.engenharia.entity.Discipline;
import ads.engenharia.entity.Professor;
import ads.engenharia.entity.Schedule;
import ads.engenharia.manager.DALManager;

/**
 * @author Lanedraex
 *
 */
public class ProfessorBO {

	private ProfessorDAO _professorDAO;
	private ProfessorCompetenceDAO _professorCompetenceDAO;
	private ProfessorScheduleDAO _professorScheduleDAO;
	private DisciplineBO _disciplineBO;

	public ProfessorBO() {

		// _professorDAO = new ProfessorDAO();
		// _professorScheduleDAO = new ProfessorScheduleDAO();
		// _professorCompetenceDAO = new ProfessorCompetenceDAO();
		// _disciplineBO = new DisciplineBO();
	}

	/**
	 * 
	 * @param professor
	 * @throws SQLException
	 */
	public void save(Professor professor) throws SQLException {

		// professor = _professorDAO.save(professor);
		// _professorCompetenceDAO.save(professor);
		// _professorScheduleDAO.save(professor);

		professor.id(DALManager.Instance().professorDAO().save(professor).id());
		DALManager.Instance().professorCompetenceDAO().save(professor);
		System.out.println(DALManager.Instance().professorScheduleDAO());
		System.out
				.println("professor dia ~" + professor.schedule().Days.size());
		DALManager.Instance().professorScheduleDAO().save(professor);

	}

	/**
	 * 
	 * @param professor
	 * @throws SQLException
	 */
	public void delete(Professor professor) throws SQLException {

		// _professorDAO.delete(professor);

		DALManager.Instance().professorDAO().delete(professor);

	}

	/**
	 * 
	 * @throws SQLException
	 */
	public void deleteAll() throws SQLException {

		// _professorDAO.deleteAll();

		DALManager.Instance().professorDAO().deleteAll();

	}

	/**
	 * 
	 * @param professor
	 * @throws SQLException
	 */
	public void update(Professor professor) throws SQLException {

		// _professorDAO.update(professor);
		// _professorScheduleDAO.update(professor);
		// _professorCompetenceDAO.update(professor);

		DALManager.Instance().professorDAO().update(professor);
		DALManager.Instance().professorScheduleDAO().update(professor);
		DALManager.Instance().professorCompetenceDAO().update(professor);

	}

	/**
	 * 
	 * @param professor
	 * @return
	 * @throws SQLException
	 */
	public Professor search(Professor professor) throws SQLException {

		// DEBUG
		System.out
				.println("DEBUG 0 search(Professor professor) ----- professor id = "
						+ professor.id());

		if (professor != null) {
			professor = DALManager.Instance().professorDAO().search(professor);
			professor.schedule(DALManager.Instance().professorScheduleDAO()
					.searchProfessorSchedule(professor));

			ArrayList<Discipline> disciplines = DALManager.Instance()
					.professorCompetenceDAO()
					.searchProfessorCompetence(professor);

			// ERROR HERE!
			// for (Discipline d : disciplines) {
			// d = DALManager.Instance().disciplineBO().search(d);
			// professor.competence().add(d);
			// }
		}
		return professor;

	}

	/**
	 * 
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Professor> searchAll() throws SQLException {

		ArrayList<Professor> professors;
		professors = DALManager.Instance().professorDAO().searchAll();

		// DEBUG
		System.out
				.println("DEBUG 1 ProfessorBO searchAll() ----- professors size = "
						+ professors.size());

		fillProfessorDetails(professors);

		return professors;

	}

	public ArrayList<Professor> searchAvailable(Discipline discipline)
			throws SQLException {

		ArrayList<Professor> professors;
		ArrayList<Professor> availableProfessors = new ArrayList<Professor>();
		professors = DALManager.Instance().professorBO().searchAll();
		Boolean hasCompentece = false;
		ArrayList<Integer> indexToRemove = new ArrayList<Integer>();

		// DEBUG
		System.out.println("DEBUG 2 before all fors --- professors.size() = "
				+ professors.size());

		for (Professor prof : professors) {

			for (int c = 0; c < prof.competence().size(); ++c) {

				// DEBUG
				System.out.println("DEBUG 3 for competence --- Professor [" + c
						+ "] name " + prof.name() + " competence: "
						+ prof.competence().get(c).name());

				if (prof.competence().get(c).id() == discipline.id()) {
					hasCompentece = true;

					// DEBUG
					System.out
							.println("DEBUG 4 for competence --- competenceId == disciplineID break!");
					break;
				}

			}

			if (!hasCompentece) {
				continue;
			}

			if (prof.workload() > 0) {

				// Checar schedule entre professor e disciplina
				for (int i = 0; i < prof.schedule().Days.size(); ++i) {

					// DEBUG
					System.out
							.println("DEBUG 5 for schedule --- Schedule.Days ["
									+ i + "] day: "
									+ prof.schedule().Days.get(i));

					// DEBUG
					System.out
							.println("DEBUG 6 for schedule --- Schedule.Time ["
									+ i + "] time: "
									+ prof.schedule().Times.get(i));

					// DEBUG
					System.out
							.println("DEBUG 7 for schedule --- Schedule.Hour ["
									+ i + "] schedule: "
									+ prof.schedule().Hours.get(i));

					if (discipline.schedule().Days.get(0).value() == prof
							.schedule().Days.get(i).value()) {

						// DEBUG
						System.out
								.println("DEBUG 8 for schedule --- professorDay == disciplineDay!");

						if (discipline.schedule().Times.get(0).value() == prof
								.schedule().Times.get(i).value()) {

							// DEBUG
							System.out
									.println("DEBUG 9 for schedule --- professorTime == disciplineTime!");

							if (discipline.schedule().Hours.get(0).value() == prof
									.schedule().Hours.get(i).value()) {

								// DEBUG
								System.out
										.println("DEBUG 10 for schedule --- professorHour == disciplineHour!");

								availableProfessors.add(prof);

								indexToRemove.add(i);
							}
						}
					}

				}

			}
		}

		// DEBUG
		for (Professor p : availableProfessors) {

			// DEBUG
			System.out.println("DEBUG 11 for availableProfessors --- name = "
					+ p.name());
		}

		return availableProfessors;
	}

	/**
	 * 
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public Professor searchById(int id) throws SQLException {

		// Professor professor;
		// professor = _professorDAO.searchById(id);
		// professor.schedule(_professorScheduleDAO
		// .searchProfessorSchedule(professor));
		//
		// ArrayList<Discipline> disciplines = _professorCompetenceDAO
		// .searchProfessorCompetence(professor);
		//
		// for (Discipline d : disciplines) {
		// d = _disciplineBO.search(d);
		// professor.competence().add(d);
		// }

		Professor professor;
		professor = DALManager.Instance().professorDAO().searchById(id);
		professor.schedule(DALManager.Instance().professorScheduleDAO()
				.searchProfessorSchedule(professor));

		ArrayList<Discipline> disciplines = DALManager.Instance()
				.professorCompetenceDAO().searchProfessorCompetence(professor);

		for (Discipline d : disciplines) {
			d = DALManager.Instance().disciplineBO().search(d);
			professor.competence().add(d);
		}

		return professor;
	}

	/**
	 * 
	 * @param name
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Professor> searchByName(String name) throws SQLException {

		// ArrayList<Professor> professors;
		// professors = _professorDAO.searchByName(name);
		//
		// for (Professor p : professors) {
		//
		// p.schedule(_professorScheduleDAO.searchProfessorSchedule(p));
		//
		// ArrayList<Discipline> disciplines = _professorCompetenceDAO
		// .searchProfessorCompetence(p);
		//
		// for (Discipline d : disciplines) {
		// d = _disciplineBO.search(d);
		// p.competence().add(d);
		// }
		// }

		ArrayList<Professor> professors;
		professors = DALManager.Instance().professorDAO().searchByName(name);

		fillProfessorDetails(professors);

		return professors;

	}

	/**
	 * 
	 * @param competence
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Professor> searchByCompetence(Discipline competence)
			throws SQLException {

		// ArrayList<Professor> professors;
		// professors = _professorDAO.searchByCompetence(competence);
		//
		// for (Professor p : professors) {
		//
		// p.schedule(_professorScheduleDAO.searchProfessorSchedule(p));
		//
		// ArrayList<Discipline> disciplines = _professorCompetenceDAO
		// .searchProfessorCompetence(p);
		//
		// for (Discipline d : disciplines) {
		// d = _disciplineBO.search(d);
		// p.competence().add(d);
		// }
		// }

		ArrayList<Professor> professors;
		professors = DALManager.Instance().professorDAO()
				.searchByCompetence(competence);

		fillProfessorDetails(professors);

		return professors;

	}

	/**
	 * 
	 * @param status
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Professor> searchByStatus(boolean status)
			throws SQLException {

		// ArrayList<Professor> professors;
		// professors = _professorDAO.searchByStatus(status);
		//
		// for (Professor p : professors) {
		//
		// p.schedule(_professorScheduleDAO.searchProfessorSchedule(p));
		//
		// ArrayList<Discipline> disciplines = _professorCompetenceDAO
		// .searchProfessorCompetence(p);
		//
		// for (Discipline d : disciplines) {
		// d = _disciplineBO.search(d);
		// p.competence().add(d);
		// }
		// }

		ArrayList<Professor> professors;
		professors = DALManager.Instance().professorDAO()
				.searchByStatus(status);

		fillProfessorDetails(professors);

		return professors;

	}

	/**
	 * @param professors
	 * @throws SQLException
	 */
	private void fillProfessorDetails(ArrayList<Professor> professors)
			throws SQLException {
		for (Professor p : professors) {

			// DEBUG
			System.out.println("DEBUG 12 for availableProfessors --- name = "
					+ p.name());

			p.schedule(DALManager.Instance().professorScheduleDAO()
					.searchProfessorSchedule(p));

			ArrayList<Discipline> disciplines = DALManager.Instance()
					.professorCompetenceDAO().searchProfessorCompetence(p);

			for (Discipline d : disciplines) {
				d = DALManager.Instance().disciplineBO().search(d);

				if (d != null) {
					p.competence().add(d);
				}
			}
		}
	}

	/**
	 * 
	 * @param day
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Professor> searchByDay(Schedule.Day day)
			throws SQLException {

		ArrayList<Professor> professors;
		professors = _professorDAO.searchByDay(day);

		for (Professor p : professors) {

			p.schedule(_professorScheduleDAO.searchProfessorSchedule(p));

			ArrayList<Discipline> disciplines = _professorCompetenceDAO
					.searchProfessorCompetence(p);

			for (Discipline d : disciplines) {
				d = _disciplineBO.search(d);
				p.competence().add(d);
			}
		}

		return professors;
	}

	/**
	 * 
	 * @param time
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Professor> searchByTime(Schedule.Time time)
			throws SQLException {

		ArrayList<Professor> professors;
		professors = _professorDAO.searchByTime(time);

		for (Professor p : professors) {

			p.schedule(_professorScheduleDAO.searchProfessorSchedule(p));

			ArrayList<Discipline> disciplines = _professorCompetenceDAO
					.searchProfessorCompetence(p);

			for (Discipline d : disciplines) {
				d = _disciplineBO.search(d);
				p.competence().add(d);
			}
		}

		return professors;
	}

	/**
	 * 
	 * @param hour
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Professor> searchByHour(Schedule.Hour hour)
			throws SQLException {

		ArrayList<Professor> professors;
		professors = _professorDAO.searchByHour(hour);

		for (Professor p : professors) {

			p.schedule(_professorScheduleDAO.searchProfessorSchedule(p));

			ArrayList<Discipline> disciplines = _professorCompetenceDAO
					.searchProfessorCompetence(p);

			for (Discipline d : disciplines) {
				d = _disciplineBO.search(d);
				p.competence().add(d);
			}
		}

		return professors;
	}

}
